package work;

public class Test04 {
    private int number = 0;
    private final Object lock = new Object();

    class ThreadA implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (lock) {
                    if (number <= 200) {
                        System.out.println(Thread.currentThread().getName() + ": " + number++);
                        lock.notify();
                        try {
                            if (number > 200) break;
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        break;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Test04 printNumbers = new Test04();
        ThreadA threadA = printNumbers.new ThreadA();
        Thread thread1 = new Thread(threadA, "Thread1");
        Thread thread2 = new Thread(threadA, "Thread2");

        thread1.start();
        thread2.start();
    }
}
